package demo.miner.system;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;

import demo.miner.ClassEntity;

public class Configure {
	
	public static void define(String directory, Class<?> clazz) {
		File dir = new File(directory);
		for (String filename : dir.list()) {
			File file = new File(dir, filename);
			if (file.isFile()) {
				FileInputStream iStream = null;
				try {
					iStream = new FileInputStream(file);
					define(clazz, iStream);
				} catch (IOException e) {
					throw new RuntimeException(e);
				} finally {
					try {
						if (iStream != null) {
							iStream.close();
						}
					} catch ( IOException e){
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static ClassEntity define(Class<?> clazz, InputStream iStream)
			throws IOException {
		
		Properties props = new Properties();
		props.load(iStream);
		
		String[] attrs = new String[props.size()];
		
		int i = 0;
		for (Entry<Object, Object> entry : props.entrySet()) {
			attrs[i++] = entry.toString();
		}
		
		return define(clazz, attrs);
	}
	
	public static ClassEntity define(Class<?> clazz, String ... attrs) {
		try {
			ClassEntity cEntity = (ClassEntity) clazz.newInstance();
			Objects.attr(cEntity, attrs);
			ClassEntity.add(cEntity);
			return cEntity;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
}
